package com.kgame5.xtank3d.model.tank {
import kgame5.enginex.EntityMessageVO;
import kgame5.enginex.EntityVO;
import kgame5.enginex.ISenceWorld;
import kgame5.enginex.comp.ICollideP2P;
import kgame5.enginex.comp.IRender;
import kgame5.enginex.comp.fsm.FSM;
import kgame5.enginex.entityimpl.LogicSpacialEntity;
import kgame5.enginex.event.FSMEvent;
import kgame5.enginex.event.GUViewEvent;
import kgame5.enginex.event.SendCommandEvent;
import kgame5.enginex.part.GFSMBatch;
import kgame5.k3d.core.obj3d.hardvo.HardVO;
import kgame5.k3d.core.obj3d.node3d.Node3D;
import kgame5.k3d.core.view3d.Mouse3D;
import kgame5.kgu.spacial.SpacialManager;
import kgame5.kutil.KKey;

import com.kgame5.xtank3d.GameEnum;
import com.kgame5.xtank3d.model.tank.comp.MyTankViewComp;
import com.kgame5.xtank3d.model.tank.fsms.barbrot.MeStateBarbActive;
import com.kgame5.xtank3d.model.tank.fsms.fire.MeStateWaitFire;
import com.kgame5.xtank3d.model.tank.fsms.fire.StateReloadShot;
import com.kgame5.xtank3d.model.tank.fsms.main.StateBorn;
import com.kgame5.xtank3d.model.tank.fsms.main.StateGood;
import com.kgame5.xtank3d.model.tank.fsms.main.StateHurt;
import com.kgame5.xtank3d.model.tank.fsms.main.StateWait;
import com.kgame5.xtank3d.model.tank.fsms.move.StateMove;
import com.kgame5.xtank3d.model.tank.fsms.move.StateStand;
import com.kgame5.xtank3d.vo.MyTankVO;

/**
 * @author kk
 * 策略
 */
public class MyTankEntity extends LogicSpacialEntity implements IRender,ICollideP2P{
	
	public var vo:MyTankVO;
	
	private var comp:MyTankViewComp;
	private var collideList:Vector.<EntityVO>;
	
	private var fsmBatch:GFSMBatch;
	private var 
		fsmMain:FSM,
		fsmMove:FSM,
		fsmFire:FSM,
		fsmBarb:FSM;
	
	public function MyTankEntity(p_vo:MyTankVO){
		super(p_vo);
		vo=p_vo;
		
		//--comp--
		comp=new MyTankViewComp(vo);
		comp.update(vo);//设置好初始位置
		
		//--collideList--
		 collideList=new Vector.<EntityVO>();
		 
		//--fsmBatch--
		fsmBatch=new GFSMBatch();
		//添加各个fsm,注意添加监听器
		
		//主fsm
		fsmMain=new FSM(GameEnum.FSMID_MAIN);
		fsmMain.addState(new StateBorn(vo));
		fsmMain.addState(new StateGood(vo));
		fsmMain.addState(new StateHurt(vo));
		fsmMain.addState(new StateWait(vo));
		_addFSM(fsmMain);
		fsmMain.addEventListener(FSMEvent.TYPE, onFSMEvent);
		
		//移动的fsm
		 fsmMove=new FSM(GameEnum.FSMID_TANK_MOVE);
		fsmMove.addState(new StateMove(vo));
		fsmMove.addState(new StateStand(vo));
		_addFSM(fsmMove);
		
		//攻击的fsm
		 fsmFire=new FSM(GameEnum.FSMID_TANK_FIRE);
		fsmFire.addState(new StateReloadShot(vo));
		fsmFire.addState(new MeStateWaitFire(vo));
		_addFSM(fsmFire);
		
		//炮塔旋转的fsm
		 fsmBarb=new FSM(GameEnum.FSMID_TANK_TURNBARB);
		fsmBarb.addState(new MeStateBarbActive(vo));
		_addFSM(fsmBarb);
		
		//默认是deactive
		this.deactive();
		
		//出生
		fsmMain.SwitchState(GameEnum.STATE_BORN);
	}

	//当fsmMain进入good的时候是这个
	private function active():void{
		fsmMove.SwitchState(GameEnum.STATE_TANK_STAND);
		fsmFire.SwitchState(GameEnum.STATE_TANK_RELOAD_SHOT);
		fsmBarb.SwitchState(GameEnum.STATE_TANK_BARB_ACTIVE);//受攻击的时候是-1
	}
	//当fsmMain离开good的时候是这个
	private function deactive():void{
		fsmMove.SwitchState(-1);
		fsmFire.SwitchState(-1);
		fsmBarb.SwitchState(-1);//受攻击的时候是-1
		//注意清除数值，这里是在STATE_GOOD.exit里做的
	}

	//---------------IP2P---------------
	//用于碰撞的HardVO.返回供GameWorldX引擎使用
	public function getCollideHardVO():HardVO{
		return _vo.hardVO;
	}
	
	
	public function addCollideItem(p_vo:EntityVO):void{
		collideList.push(p_vo);
	}
	public function getCollideList():Vector.<EntityVO>{
		return collideList;
	}
	public function clearCollideList():void{
		collideList=new Vector.<EntityVO>();
	}
	//---------------IP2E-----------------
	override public function setSpacialManager(spacialManager:SpacialManager):void{
		super.setSpacialManager(spacialManager);
		fsmBatch.setSpacialManager(spacialManager);
	}
	
	//---------------Logic--------------
	override public function setSenceWorld(senceWorld:ISenceWorld):void{
		super.setSenceWorld(senceWorld);
		fsmBatch.setSenceWorld(senceWorld);
	}
	override public function updateLogic():void{
		updateInput();
		fsmBatch.update();
		spacialManager.ProcessMove4(vo.hardVO);
	}
	//-切换fsm状态
	override public function SwitchFSMState(fsmID:int,stateID:int,data:Object=null):void{
		fsmBatch.SwitchFSMState(fsmID, stateID,data);
	}
	override public function handleEntityMessage(mvo:EntityMessageVO):void{
		
	}
	//---------------IRender-----------------
	//简单情况不需要使用viewComp
	
	public function updateView():void{
		comp.update(vo);
	}
	public function getNode3D():Node3D{
		return comp.getNode3D();
	}
	
	public function getShadow3D():Node3D{
		return comp.getShadow3D();
	}
	
	//-------------事件处理函数-------------
	//添加fsm的事件处理，有空定义一个好的FSMEvent
	private function onFSMEvent(e:FSMEvent):void{
		switch(e.name){
			case GameEnum.MAIN_GOOD_ENTER:
			this.active();
			break;
			case GameEnum.MAIN_GOOD_EXIT:
			this.deactive();
			break;
			
			case GameEnum.TANK_FIRE:
			comp.fire();
			break;
			
			case GameEnum.BORN_START:
			comp.bornStart();
			break;
			case GameEnum.BORN_END:
			comp.bornEnd();
			break;
			
			case GameEnum.HURT_START:
			comp.hurtStart(e.data as int);
			break;
			case GameEnum.HURT_END:
			comp.hurtEnd();
			break;
			
			case GameEnum.RELOAD_START:
			comp.reloadStart();
			break;
			case GameEnum.RELOAD_END:
			comp.reloadEnd();
			break;
		}
	}

	//这个是个EventLister，被添加到viewComp的事件监听中,处理view发送来的事件
	private function onViewCompEvent(e:GUViewEvent):void{
		/*var data:XX=e.getBody() as XX;
		 * switch(e.getName()){
		 * case NNN:
		 * 处理
		 * break;
		 * }
		 */
	}

	//得到fsm命令发送Command.处理view请求发送command
	protected function onSendCommandEvent(e:SendCommandEvent):void{
		this.sendCommand(e.commandName, e.data);//被重新生成一个command.关系不大，command不是每帧都调用
	}
	
	//----------------------实用工具-------------------
	//添加fsm
	private function _addFSM(fsm:FSM):void{
		fsmBatch.addFSM(fsm);
		fsm.addEventListener(FSMEvent.TYPE, onFSMEvent);
		fsm.addEventListener(SendCommandEvent.TYPE, onSendCommandEvent);
	}
	//WASD JK和上下左右空格和ctr都用
	private function updateInput():void{
		
		//移动
		if(KKey.isDown_W || KKey.isDown_UP) {
			vo.input.dir= SpacialManager.DIR_UP;
		}else if(KKey.isDown_S|| KKey.isDown_DOWN){
			vo.input.dir= SpacialManager.DIR_DOWN;
		}else if(KKey.isDown_A|| KKey.isDown_LEFT){
			vo.input.dir= SpacialManager.DIR_LEFT;
		}else if(KKey.isDown_D|| KKey.isDown_RIGHT){
			vo.input.dir= SpacialManager.DIR_RIGHT;
		}else{
			vo.input.dir= SpacialManager.DIR_NULL;
		}
		
		//开火
		vo.input.fire=KKey.isMouseDown;//KKey.isDown_SPACE || KKey.isDown_J;//远程武器
		
		//炮塔旋转
		vo.input.mouse3DX= Mouse3D.Mouse3DX;
		vo.input.mouse3DZ= Mouse3D.Mouse3DZ;
	}
}//end of class
}
